package com.cuxiaoyi.action;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

import com.cuxiaoyi.Constants;
import com.cuxiaoyi.db.DBUtils;
import com.cuxiaoyi.db.Dao;
import com.cuxiaoyi.taobao.TaobaoProxy;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.domain.Promotion;
import com.taobao.api.request.MarketingPromotionAddRequest;
import com.taobao.api.response.MarketingPromotionAddResponse;

public class AddPromotionAction extends ActionSupport implements Constants{

	static Logger _log = Logger.getLogger(AddPromotionAction.class.getName());
	
	private MarketingPromotionAddRequest promotionAddRequest;
	
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		long userId = Long.parseLong((String)session.get(USER_ID));
		String sessionId = (String)session.get(TOP_SESSION);
		_log.info("type: " + promotionAddRequest.getDiscountType());
		_log.info("value: " + promotionAddRequest.getDiscountValue());
		_log.info("start: " + promotionAddRequest.getStartDate());
		_log.info("end: " + promotionAddRequest.getEndDate());
		_log.info("numIids: " + promotionAddRequest.getNumIids());
		String numIids = promotionAddRequest.getNumIids();
		String[] ids = StringUtils.split(numIids, ',');
		Connection conn = null;
		try
		{	
			conn = DBUtils.getConnection();
			for (String numIid : ids)
			{
				Promotion p = Dao.INSTANCE.getPromotion(Long.parseLong(numIid), conn);
				if (p == null)
				{
					promotionAddRequest.setNumIids(numIid);
					MarketingPromotionAddResponse rsp = TaobaoProxy.getClient().execute(promotionAddRequest, sessionId);
					if (rsp.isSuccess())
					{
						List<Promotion> promotions = rsp.getPromotions();
						storePromotions(promotions, userId, conn);
					}
					else
					{
						error(rsp);
					}
				}
			}
		}
		catch (Exception e)
		{
			_log.log(Level.SEVERE, "", e);
		}
		finally
		{
			DBUtils.close(conn);
		}
		return SUCCESS;
	}

	private void storePromotions(List<Promotion> promotions, long userId, Connection conn)
	{
		for (Promotion promotion : promotions)
		{
			_log.info("promotionId: " + promotion.getPromotionId());
			promotion.setDiscountType(promotionAddRequest.getDiscountType());
			promotion.setDiscountValue(promotionAddRequest.getDiscountValue());
			promotion.setStartDate(promotionAddRequest.getStartDate());
			promotion.setEndDate(promotionAddRequest.getEndDate());
			promotion.setTagId(promotionAddRequest.getTagId());
			promotion.setPromotionTitle(promotionAddRequest.getPromotionTitle());
			promotion.setPromotionDesc(promotionAddRequest.getPromotionDesc());
			try
			{
				Dao.INSTANCE.addPromotion(userId, promotion, conn);
			}
			catch (Exception e)
			{
				_log.log(Level.SEVERE, "", e);
			}
		}
	}
	
	public void setPromotionAddRequest(MarketingPromotionAddRequest promotionAddRequest) {
		this.promotionAddRequest = promotionAddRequest;
	}

	public MarketingPromotionAddRequest getPromotionAddRequest() {
		return promotionAddRequest;
	}
	
	private void error(TaobaoResponse rsp)
	{
		_log.info(rsp.getErrorCode() + " - " + rsp.getMsg() + " - " + rsp.getSubCode() + " - " + rsp.getSubMsg());
	}
}
